﻿using System;
using System.Data;
using System.Collections;
using Titan.SqlTracer;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Titan
{
    /// <summary>
    /// 操作数据库的类，包含一组常用的Helper方法和一组ORM对象操作的方法。内部承载了一个IDbConnection数据库连接对象
    /// </summary>
    public interface IDbSession : IDisposable
    {

        /// <summary>
        /// 获取内部承载的sql解析器，针对不同类型的数据库有不同的解析器
        /// </summary>
        ISqlProvider SqlProvider { get; }

        /// <summary>
        /// 获取sql语句跟踪器
        /// </summary>
        ISqlTracer[] SqlTracers { get; }

        /// <summary>
        /// 获取内部承载的数据库连接对象
        /// </summary>
        IDbConnection Connection { get; }

        /// <summary>
        /// 获取执行BeginTransaction方法后产生的IDbTransaction对象
        /// </summary>
        IDbTransaction Transaction { get; }

        /// <summary>
        /// 打开数据库会话，会将内部承载的Connection对象打开
        /// </summary>
        void Open();
        /// <summary>
        /// 关闭数据库会话，会将内部承载的Connection对象关闭
        /// </summary>
        void Close();

        /// <summary>
        /// 开始一个数据库事务，调用后可以通过Transaction属性获取到事务实例
        /// </summary>
        void BeginTransaction();
        /// <summary>
        /// 开始一个数据库事务，调用后可以通过Transaction属性获取到事务实例
        /// </summary>
        /// <param name="iso">指定连接事务的锁定行为</param>
        void BeginTransaction(IsolationLevel iso);
        /// <summary>
        /// 提交事务，提交后会自动关闭事务
        /// </summary>
        void Commit();
        /// <summary>
        /// 回滚事务，回滚完毕后会自动关闭事务
        /// </summary>
        void Rollback();





        #region 常规helper

        /// <summary>
        /// 判断记录是否存在 ,例如传入"select top 1 1 from t"
        /// </summary>
        /// <param name="sql">Sql语句</param> 
        /// <returns></returns>
        bool RecordExists(string sql);
        /// <summary>
        /// 判断记录是否存在 ,例如传入"select top 1 1 from t"
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        bool RecordExists(IDbCommand command);
        /// <summary>
        /// 获取第一行DataRow
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <returns></returns>
        DataRow ExecuteFirstDataRow(string sql);
        /// <summary>
        /// 获取第一行DataRow
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        DataRow ExecuteFirstDataRow(IDbCommand command);
        /// <summary>
        /// 获取一个DataTable
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <returns></returns>
        DataTable ExecuteDataTable(string sql);
        /// <summary>
        /// 获取一个DataTable
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(IDbCommand command);
        /// <summary>
        /// 分页获取DataTable
        /// </summary>
        /// <param name="command">Sql语句</param>
        /// <param name="pageSize">每页多少行数据</param>
        /// <param name="pageIndex">第几页，从1开始</param>
        /// <returns></returns>
        DataTable ExecuteDataTable(IDbCommand command, int pageSize, int pageIndex);
        /// <summary>
        /// 获取一个DataSet
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <returns></returns>
        DataSet ExecuteDataSet(string sql);
        /// <summary>
        /// 获取一个DataSet
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(IDbCommand command);
        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <returns></returns>
        IDataReader ExecuteReader(string sql); 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbCommand command);
        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <param name="command"></param>
        /// <param name="behavior"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbCommand command, CommandBehavior behavior);
        /// <summary>
        /// 分页获取DataReader
        /// </summary>
        /// <param name="command">Sql语句</param>
        /// <param name="pageSize">每页多少行数据</param>
        /// <param name="pageIndex">第几页，从1开始</param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbCommand command, int pageSize, int pageIndex);
        /// <summary>
        /// 执行一条Sql语句
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <returns>影响行数</returns>
        int ExecuteNonQuery(string sql);
        /// <summary>
        /// 执行Command
        /// </summary>
        /// <param name="command">IDbCommand对象</param>
        /// <returns>影响行数</returns>
        int ExecuteNonQuery(IDbCommand command);
        /// <summary>
        /// 获取第一行第一列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        object ExecuteScalar(string sql);
        /// <summary>
        /// 获取第一行第一列的值
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        object ExecuteScalar(IDbCommand command);
        #endregion


        #region ORM对象操作

        /// <summary>
        /// 将数据库中的数据类型转换为常规类型
        /// </summary>
        /// <typeparam name="T">期望的结果类型</typeparam>
        /// <param name="databaseValue">数据库中的值，如DataReader结果中的值或CommandParameter参数中的值</param>
        /// <returns></returns>
        T ConvertDatabaseValue<T>(object databaseValue);


        #region Insert

        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="entity">待添加的对象</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(object entity);



        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="obj">待添加的对象</param>
        /// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param> 
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(object obj, params string[] excludePropertyNames);



        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="entity">待添加的对象</param>
        /// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param> 
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(object entity, IEnumerable<string> excludePropertyNames);



        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="obj">待添加的对象</param>
        /// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param> 
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(object obj, params IPropertyExpression[] excludePropertys);



        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="entity">待添加的对象</param>
        /// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param> 
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(object entity, IEnumerable<IPropertyExpression> excludePropertys); 


  

        ///// <summary>
        ///// 将对象转化成insert语句，往数据库添加一条记录
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="obj">待添加的对象</param>
        ///// <returns>数据库中返回的影响行数</returns>
        //int Insert(IDictionary<string, string> tableMapping, object obj);


        ///// <summary>
        ///// 将对象转化成insert语句，往数据库添加一条记录
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="obj">待添加的对象</param>
        ///// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param>
        ///// <returns>数据库中返回的影响行数</returns>
        //int Insert(IDictionary<string, string> tableMapping, object obj, params string[] excludePropertyNames);


        /// <summary>
        /// 将对象转化成insert语句，往数据库添加一条记录
        /// </summary>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="obj">待添加的对象</param>
        /// <param name="excludePropertyNames">生成sql语句时，强制不生成该参数指定的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Insert(IDictionary<string, string> tableMapping, object obj, IEnumerable<string> excludePropertyNames = null);

         


        

 

        #endregion



        #region Update

        /// <summary>
        /// 修改数据库，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param> 
        /// <returns>数据库中返回的影响行数</returns>
        int Update(object entity);

        /// <summary>
        /// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="updatePropertyNames">需要修改的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Update(object entity, IEnumerable<string> updatePropertyNames); 


        /// <summary>
        /// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="updatePropertyNames">需要修改的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Update(object entity, params string[] updatePropertyNames);



        /// <summary>
        /// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="updatePropertyNames">需要修改的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Update(object entity, IEnumerable<IPropertyExpression> updatePropertyNames);


        /// <summary>
        /// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="updatePropertyNames">需要修改的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Update(object entity, params IPropertyExpression[] updatePropertyNames); 









        ///// <summary>
        ///// 只修改数据库所有列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="entity">实体对象的实例，注意设置对象的主键值</param> 
        ///// <returns>数据库中返回的影响行数</returns>
        //int Update(IDictionary<string, string> tableMapping, object entity);


        ///// <summary>
        ///// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        ///// <param name="updatePropertyNames">需要修改的列</param>
        ///// <returns>数据库中返回的影响行数</returns>
        //int Update(IDictionary<string, string> tableMapping, object entity, params string[] updatePropertyNames);


        /// <summary>
        /// 只修改数据库中指定的列，将对象转化为update语句，修改数据库中的一条记录。注意设置传入对象的主键值
        /// </summary>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="updatePropertyNames">需要修改的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Update(IDictionary<string, string> tableMapping, object entity, IEnumerable<string> updatePropertyNames);
         

         





        #endregion


        #region Delete


        /// <summary>
        /// 将对象转化成delete语句，从数据库删除一条记录，注意设置对象主键值
        /// </summary>
        /// <param name="entity">实体对象的实例</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Delete(object entity);


        /// <summary>
        /// 将对象转化成delete语句，从数据库删除一条记录，注意设置对象主键值
        /// </summary>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="entity">实体对象的实例</param>
        /// <returns>数据库中返回的影响行数</returns>
        int Delete(IDictionary<string, string> tableMapping, object entity);


        #endregion

        #region BatchUpdate
        /// <summary>
        /// 根据指定条件批量更新记录，将所有符合指定条件的记录的指定列全部更新为指定对象的相应值
        /// </summary>
        /// <param name="entity">需要更新的属性值</param>
        /// <param name="conditionExpression">更新条件</param>
        /// <param name="updatePropertyNames">需要更新的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchUpdate(object entity, IConditionExpression conditionExpression, IEnumerable<string> updatePropertyNames); 
        /// <summary>
        /// 根据指定条件批量更新记录，将所有符合指定条件的记录的指定列全部更新为指定对象的相应值
        /// </summary>
        /// <param name="entity">需要更新的属性值</param>
        /// <param name="conditionExpression">更新条件</param>
        /// <param name="updatePropertyNames">需要更新的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchUpdate(object entity, IConditionExpression conditionExpression, params string[] updatePropertyNames); 



        /// <summary>
        /// 根据指定条件批量更新记录，将所有符合指定条件的记录的指定列全部更新为指定对象的相应值
        /// </summary>
        /// <param name="entity">需要更新的属性值</param>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="conditionExpression">更新条件</param>
        /// <param name="updatePropertyNames">需要更新的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchUpdate(IDictionary<string, string> tableMapping, object entity, IConditionExpression conditionExpression, IEnumerable<string> updatePropertyNames); 
        /// <summary>
        /// 根据指定条件批量更新记录，将所有符合指定条件的记录的指定列全部更新为指定对象的相应值
        /// </summary>
        /// <param name="entity">需要更新的属性值</param>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="conditionExpression">更新条件</param>
        /// <param name="updatePropertyNames">需要更新的列</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchUpdate(IDictionary<string, string> tableMapping, object entity, IConditionExpression conditionExpression, params string[] updatePropertyNames); 

        #endregion


        #region BatchDelete
        /// <summary>
        /// 从数据库中批量删除数据
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="conditionExpression">删除的条件</param>
        /// <returns></returns>
        int BatchDelete<T>(IConditionExpression conditionExpression);
        int BatchDelete(Type entityType, IConditionExpression conditionExpression);
        int BatchDelete(EntityType entityType, IConditionExpression conditionExpression);
        /// <summary>
        /// 从数据库中批量删除数据
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="expression">删除的条件</param>
        /// <returns></returns>
        int BatchDelete<T>(Expression<Func<T,bool>> expression);



        /// <summary>
        /// 从数据库中批量删除数据
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param> 
        /// <param name="conditionExpression">删除的条件</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchDelete<T>(IDictionary<string, string> tableMapping, IConditionExpression conditionExpression);
        int BatchDelete(IDictionary<string, string> tableMapping, Type entityType, IConditionExpression conditionExpression);
        int BatchDelete(IDictionary<string, string> tableMapping, EntityType entityType, IConditionExpression conditionExpression);
        /// <summary>
        /// 从数据库中批量删除数据
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param> 
        /// <param name="expression">删除的条件</param>
        /// <returns>数据库中返回的影响行数</returns>
        int BatchDelete<T>(IDictionary<string, string> tableMapping, Expression<Func<T, bool>> expression);

        #endregion

        #region Select
        /// <summary>
        /// 根据主键从数据库获取对象，以提高性能
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param> 
        /// <returns>是否找到记录</returns>
        bool Select(object entity);
        /// <summary>
        /// 根据主键从数据库获取对象指定列的值，以提高性能
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="outputPropertyNames">需要输出的列</param>
        /// <returns>是否找到记录</returns>
        bool Select(object entity, IEnumerable<string> outputPropertyNames); 

        /// <summary>
        /// 根据主键从数据库获取对象指定列的值，以提高性能
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="outputPropertyNames">需要输出的列</param>
        /// <returns>是否找到记录</returns>
        bool Select(object entity, params string[] outputPropertyNames);
        /// <summary>
        /// 根据主键从数据库获取对象指定列的值，以提高性能
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="outputPropertyNames">需要输出的列</param>
        /// <returns>是否找到记录</returns>
        bool Select(object entity, IEnumerable<IPropertyExpression> outputPropertyNames);

        /// <summary>
        /// 根据主键从数据库获取对象指定列的值，以提高性能
        /// </summary>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="outputPropertyNames">需要输出的列</param>
        /// <returns>是否找到记录</returns>
        bool Select(object entity, params IPropertyExpression[] outputPropertyNames); 






        ///// <summary>
        ///// 根据主键从数据库获取对象指定列的值，以提高性能
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        ///// <returns>是否找到记录</returns>
        //bool Select(IDictionary<string, string> tableMapping, object entity);


        /// <summary>
        /// 根据主键从数据库获取对象指定列的值，以提高性能
        /// </summary>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        /// <param name="outputPropertyNames">只返回指定的属性</param>
        /// <returns>是否找到记录</returns>
        bool Select(IDictionary<string, string> tableMapping, object entity, IEnumerable<string> outputPropertyNames); 
        ///// <summary>
        ///// 根据主键从数据库获取对象指定列的值，以提高性能
        ///// </summary>
        ///// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        ///// <param name="entity">实体对象的实例，注意设置对象的主键值</param>
        ///// <param name="outputPropertyNames">只返回指定的属性</param>
        ///// <returns>是否找到记录</returns>
        //bool Select(IDictionary<string, string> tableMapping, object entity, params string[] outputPropertyNames); 

        #endregion

        #region SelectCollection
        /// <summary>
        /// 从数据库返回记录集合
        /// </summary>
        /// <typeparam name="T">指定要返回的类型</typeparam>
        /// <param name="list">返回结果会存储在本对象内</param> 
        /// <param name="queryExpression">查询表达式</param>
        /// <returns>返回全部匹配的记录数，如果returnMatchedCount为false则返回否则返回集合元素个数</returns>
        int SelectCollection<T>(IList<T> list, IQueryExpression queryExpression);



        int SelectCollection(Type entityType, IList list, IQueryExpression queryExpression);
        int SelectCollection(EntityType entityType, IList list, IQueryExpression queryExpression);

        /// <summary>
        /// 从数据库返回记录集合
        /// </summary>
        /// <typeparam name="T">指定要返回的类型</typeparam>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="list">返回结果会存储在本对象内</param>
        /// <param name="queryExpression">查询表达式</param>
        /// <returns>返回全部匹配的记录数，如果returnMatchedCount为false则返回否则返回集合元素个数</returns>
        int SelectCollection<T>(IDictionary<string, string> tableMapping, IList<T> list, IQueryExpression queryExpression);
        int SelectCollection(IDictionary<string, string> tableMapping, Type entityType, IList list, IQueryExpression queryExpression);
        int SelectCollection(IDictionary<string, string> tableMapping, EntityType entityType, IList list, IQueryExpression queryExpression);
        #endregion

        #region SelectCount
        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="conditionExpression">条件</param>
        /// <returns>符合条件的记录数</returns>
        int SelectCount<T>(IConditionExpression conditionExpression);
        int SelectCount(Type entityType, IConditionExpression conditionExpression);
        int SelectCount(EntityType entityType, IConditionExpression conditionExpression);
        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">内部根据此参数动态生成表名称，适用于表名称是动态的情况</typeparam>
        /// <param name="expression">条件</param>
        /// <returns>符合条件的记录数</returns>
        int SelectCount<T>(Expression<Func<T, bool>> expression);



        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">内部根据此参数动态生成表名称，适用于表名称是动态的情况</typeparam>
        /// <param name="tableMapping">对象的类型</param>
        /// <param name="conditionExpression">条件</param>
        /// <returns>符合条件的记录数</returns>
        int SelectCount<T>(IDictionary<string, string> tableMapping, IConditionExpression conditionExpression);
        int SelectCount(IDictionary<string, string> tableMapping, Type entityType, IConditionExpression conditionExpression);
        int SelectCount(IDictionary<string, string> tableMapping, EntityType entityType, IConditionExpression conditionExpression);

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">内部根据此参数动态生成表名称，适用于表名称是动态的情况</typeparam>
        /// <param name="tableMapping">对象的类型</param>
        /// <param name="expression">条件</param>
        /// <returns>符合条件的记录数</returns>
        int SelectCount<T>(IDictionary<string, string> tableMapping, Expression<Func<T,bool>> expression);

        #endregion

        #region Exists


        bool Exists(Type entityType, IConditionExpression conditionExpression);
        bool Exists(EntityType entityType, IConditionExpression conditionExpression); 
        bool Exists(IDictionary<string, string> tableMapping, Type entityType, IConditionExpression conditionExpression);
        bool Exists(IDictionary<string, string> tableMapping, EntityType entityType, IConditionExpression conditionExpression);


        /// <summary>
        /// 对象是否存在
        /// </summary>
        /// <param name="entity">注意设置主键</param>
        /// <returns></returns>
        bool Exists(object entity);


        /// <summary>
        /// 对象是否存在
        /// </summary>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param>
        /// <param name="entity">注意设置主键</param>
        /// <returns></returns>
        bool Exists(IDictionary<string, string> tableMapping, object entity);



        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="conditionExpression">条件</param>
        /// <returns>记录是否存在</returns>
        bool Exists<T>(IConditionExpression conditionExpression);

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="expression">条件</param>
        /// <returns>记录是否存在</returns>
        bool Exists<T>(Expression<Func<T, bool>> expression);


        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param> 
        /// <param name="conditionExpression">条件</param>
        /// <returns>记录是否存在</returns>
        bool Exists<T>(IDictionary<string, string> tableMapping, IConditionExpression conditionExpression);

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="tableMapping">内部根据此参数动态生成表名称，适用于表名称是动态的情况</param> 
        /// <param name="expression">条件</param>
        /// <returns>记录是否存在</returns>
        bool Exists<T>(IDictionary<string, string> tableMapping, Expression<Func<T, bool>> expression);

        #endregion

        #region Command
        /// <summary>
        /// 执行命令实体，并将结果集填充到集合对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <param name="lists"></param>
        /// <returns>command中返回的数据库影响行数</returns>
        void ExecuteReaderAndFill(object command, IList[] lists);
        /// <summary>
        /// 执行命令实体，并将结果集填充到集合对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <param name="lists"></param>
        /// <param name="entityTypes"></param>
        /// <returns>command中返回的数据库影响行数</returns>
        void ExecuteReaderAndFill(object command, IList[] lists, EntityType[] entityTypes);

         
        /// <summary>
        /// http://msdn.microsoft.com/en-us/library/ms971497, you must close the datareader before you process the output parameters
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(object command);

        /// <summary>
        /// 执行命令实体类
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        int ExecuteNonQuery(object command);
        #endregion

        #endregion
    }
}
